home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr34 / blowpas.zip / BLOWF_E.ASM < prev    next >
Assembly Source File  |  1994-12-31  |  4KB  |  162 lines

  1. ; Assembly implementation of BlowFish E function.
  2. ;
  3. ; Original algoritm by Bruce Schneier
  4. ; First assembly implementation by John Lots and Walter van Holst
  5. ; Assembly rewrite by Jeroen Pluimers
  6. ;
  7. ; This code is hereby donated to the public domain
  8. ;
  9. ; Version history of rewritten code:
  10. ;   19941230 1.00.00 jwp - implemented assembly code from scratch
  11. ;
  12. ; Original Pascal code:
  13. ;   Type
  14. ;     TPArray = array[1..18] of Longint;
  15. ;     TSBox = array[1..4,0..255] of Longint;
  16. ;   procedure BlowEncrypt(var paramXl,paramXr:LongInt; PArray:TPArray; SBox: TSBox);
  17. ;   var
  18. ;     Xl,Xr, Temp: Longint;
  19. ;     i: Byte;
  20. ;   begin
  21. ;     Xl := paramXl;
  22. ;     Xr := paramXr;
  23. ;     for i := 1 to 16 do begin
  24. ;       Xl:=Xl XOR P[i];
  25. ;       Xr:=F(Xl,S) XOR Xr;
  26. ;       Temp:=Xl;
  27. ;       Xl:=Xr;
  28. ;       Xr:=Temp;
  29. ;     end;
  30. ;     Temp:=Xl;
  31. ;     Xl:=Xr;
  32. ;     Xr:=Temp;
  33. ;     Xr:=Xr XOR P[17];
  34. ;     Xl:=Xl XOR P[18];
  35. ;     paramXl := Xl;
  36. ;     paramXr := Xr;
  37. ;   end;
  38.  
  39. Ideal
  40. P386
  41. Model   TPascal
  42.  
  43. CodeSeg
  44.  
  45.   Struc TPArray
  46.     contents      dd      18 dup(?)
  47.   EndS
  48.  
  49.   Struc TSBox
  50.     box1          dd      256 dup(?)
  51.     box2          dd      256 dup(?)
  52.     box3          dd      256 dup(?)
  53.     box4          dd      256 dup(?)
  54.   EndS
  55.  
  56.   Proc    BlowEncrypt Far Xl: DWord, Xr: DWord, PArray: DWord, SBox: DWord
  57.   Public  BlowEncrypt
  58.   ; NOTE:
  59.   ;  - This routine is FAR
  60.  
  61.   Extrn   F: Near
  62.   ; Volgens de handleiding moet dit ook kunnen, maar het werkt niet:
  63.   ; ProcDesc F Near Input: DWord, SBox: DWord
  64.  
  65.   ; stack frame is automatically generated
  66.  
  67.           push    ds
  68.  
  69.           ; load the passed data
  70.           lds     si,  [Xl]
  71.           mov     ebx, [ds:si]
  72.           lds     si,  [Xr]
  73.           mov     edx, [ds:si]
  74.           lds     si,  [PArray]
  75.           les     di,  [SBox]
  76.  
  77.   ; ds:si = PArray[i]
  78.   ; es:di = SBox
  79.   ; B = Xl
  80.   ; D = Xr
  81.  
  82.   ; note: each time we call F, we must save ds:si, es:di, B and D
  83.  
  84.           mov     cx,  16
  85.  
  86.   @@Loop:
  87.  
  88.           ; Xl := Xl XOR PArray[i];
  89.  
  90.           pushf   ; be nice to software that expects the D flag to be saved
  91.           cld
  92.           lodsd   ; Get PArray[i] into eax and increment si by 4
  93.           popf
  94.           xor     ebx, eax
  95.  
  96.           ; Xr := F(Xl,SBox) XOR Xr;
  97.  
  98.           push    cx
  99.           push    ebx   ; new Xl
  100.           push    edx   ; old Xr
  101.           push    ds
  102.           push    si
  103.           push    es
  104.           push    di
  105.  
  106.           push    ebx
  107.           push    es
  108.           push    di
  109.           call    F
  110.  
  111.           ; get dx:ax into edx
  112.           shl     edx, 16
  113.           mov     dx,  ax
  114.  
  115.           pop     di
  116.           pop     es
  117.           pop     si
  118.           pop     ds
  119.           pop     eax     ; old Xr
  120.           pop     ebx     ; new Xl
  121.           pop     cx
  122.  
  123.           xor     edx, eax   ; new Xr
  124.  
  125.           ; Swap new Xl and new Xr
  126.  
  127.           xchg    ebx, edx
  128.  
  129.           loop    @@Loop
  130.  
  131.           ; Swap new Xl and new Xr back
  132.  
  133.           xchg    ebx, edx
  134.  
  135.           ; Xr:=Xr XOR PArray[17];
  136.           ; Xl:=Xl XOR PArray[18];
  137.  
  138.           pushf   ; be nice to software that expects the D flag to be saved
  139.           cld
  140.           lodsd   ; get PArray[17]
  141.           xor     edx, eax
  142.           lodsd   ; get PArray[18]
  143.           xor     ebx, eax
  144.           popf
  145.  
  146.           lds     si,  [Xl]
  147.           mov     [ds:si], ebx
  148.           lds     si,  [Xr]
  149.           mov     [ds:si], edx
  150.  
  151.           pop     ds
  152.  
  153.           ret
  154.  
  155.   ; stack cleanup is automatically generated
  156.  
  157.   EndP ; BlowEncrypt
  158.  
  159.  
  160. EndS  ; CodeSeg
  161.  
  162. End   ; Source